﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Restaurant
{
    /// <summary>
    /// Interaction logic for UserPassword.xaml
    /// </summary>
    public partial class UserPassword : Window
    {
        private string name;
        public UserPassword()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (GetPasswordByID() != "")
            {   
                MainWindow mainWindow = new MainWindow();
                mainWindow.UserName.Content = "User: " + name;
                mainWindow.Show();
                this.Close();
            }
            else 
            {
                MessageBox.Show("Incorrect Passord or ID");
                LoginWindow loginWindow = new LoginWindow();
                loginWindow.Show();
                this.Close();
            }
        }

        private string GetPasswordByID()
        {
            //bool hasID = false;
            StringBuilder workersList = FileReader.ReadFile("../../workers.txt", "error.log");
            string[] workers = workersList.ToString().Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
            string pass="";
            name = "";
            foreach (var worker in workers)
            {
                string[] token = worker.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                string num = token[0].Trim();
                pass = token[1].Trim();
                if (num == txtID.Text)
                {
                    //hasID = true;
                    if (pass == passBox.Password)
                    {
                        name = token[2].Trim();
                        return pass;
                    }
                }
            }
            return "";
        }       
    }
}
